//
//  LocalRepairHistoryView.m
//  ICrEAM
//
//  Created by ysw on 2016/11/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "LocalRepairHistoryView.h"
#import "LocalRepairTableViewCell.h"
#import "ICMRepairMaintenanceModel.h"
#import "ICMRepairPartsModel.h"

#define HeadHeight ActualHeight(48)
#define HeadLabelYDistance ActualHeight(10)
#define HeadLineYDistance ActualHeight(20)

@interface LocalRepairHistoryView ()<UITableViewDelegate,UITableViewDataSource,LocalRepairTableViewCellDeleagte>

@property (nonatomic, strong) UITableView *localTableView;

@property (nonatomic, strong) ICMRepairOrderArrayModel *searchLocalArray;

@property (nonatomic, assign) BOOL hasNext;

@property (nonatomic, assign) int pageNo;

@end

@implementation LocalRepairHistoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupTableHeader];
        [self setupTableView];
        
    }
    return self;
}

+ (instancetype)createLocalRepairHistoryViewWithFrame:(CGRect)frame {
    return [[LocalRepairHistoryView alloc]initWithFrame:frame];
}

- (void)setupTableHeader {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, HeadHeight)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UILabel *createDateLabel = [self createLabelWithText:@"开单日期" frame:CGRectMake(0, HeadLabelYDistance, ActualWidth(220), HeadHeight)];
    [headView addSubview:createDateLabel];
    createDateLabel.centerY = HeadHeight/2;
    
    UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(createDateLabel.rightEdge, 0, 1, HeadHeight)];
    firstSep.backgroundColor = SepColor_gray;
    [headView addSubview:firstSep];    
    
    UILabel *mileageLabel = [self createLabelWithText:@"进厂行驶里程" frame:CGRectMake(firstSep.rightEdge, HeadLabelYDistance, ActualWidth(225), HeadHeight)];
    [headView addSubview:mileageLabel];
    mileageLabel.centerY = HeadHeight/2;
    
    UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(mileageLabel.rightEdge, 0, 1, HeadHeight)];
    secondSep.backgroundColor = SepColor_gray;
    [headView addSubview:secondSep];
    
    UILabel *repairTypeLabel = [self createLabelWithText:@"维修类型" frame:CGRectMake(secondSep.rightEdge, HeadLabelYDistance, ActualWidth(165), HeadHeight)];
    [headView addSubview:repairTypeLabel];
    repairTypeLabel.centerY = HeadHeight/2;
    
    UIView *thirdSep = [[UIView alloc] initWithFrame:CGRectMake(repairTypeLabel.rightEdge, 0, 1, HeadHeight)];
    thirdSep.backgroundColor = SepColor_gray;
    [headView addSubview:thirdSep];
    
    UILabel *workPriceLabel = [self createLabelWithText:@"工时费" frame:CGRectMake(thirdSep.rightEdge, HeadLabelYDistance, ActualWidth(155), HeadHeight)];
    [headView addSubview:workPriceLabel];
    workPriceLabel.centerY = HeadHeight/2;
    
    UIView *fourthSep = [[UIView alloc] initWithFrame:CGRectMake(workPriceLabel.rightEdge, 0, 1, HeadHeight)];
    fourthSep.backgroundColor = SepColor_gray;
    [headView addSubview:fourthSep];
    
    UILabel *materialPriceLabel = [self createLabelWithText:@"材料费" frame:CGRectMake(fourthSep.rightEdge, HeadLabelYDistance, ActualWidth(255), HeadHeight)];
    [headView addSubview:materialPriceLabel];
    materialPriceLabel.centerY = HeadHeight/2;
    
    UIView *horizontalSep = [[UIView alloc] initWithFrame:CGRectMake(0 , HeadHeight-1, ScreenWidth, 1)];
    horizontalSep.backgroundColor = BackgroundColor_Tanslucent_black;
    [headView addSubview:horizontalSep];
    
    [self addSubview:headView];
    

}

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = TitleFont;
    label.textColor = ContentTextColor_Gray;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

- (void)setupTableView {
    self.localTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, HeadHeight, self.size.width, self.size.height-HeadHeight)];
    self.localTableView.delegate = self;
    self.localTableView.dataSource = self;
    self.localTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.localTableView];
    [self.localTableView registerClass:[LocalRepairTableViewCell class] forCellReuseIdentifier:@"LocalRepairCell"];
}

#pragma mark -- tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchLocalArray.TT_REPAIR_ORDER.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LocalRepairTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocalRepairCell"];
    if (!cell) {
        cell = [[LocalRepairTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LocalRepairCell"];
    }
    ICMRepairOrderModel *model = self.searchLocalArray.TT_REPAIR_ORDER[indexPath.row];

    [cell setupLabelsWithModel:model];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICMRepairOrderModel *model = self.searchLocalArray.TT_REPAIR_ORDER[indexPath.row];
    if (model.isShow) {
        CGFloat detailViewHeight = ActualHeight(86)+ActualHeight(50)*model.TT_RO_LABOUR.count+1+(model.TT_RO_REPAIR_PART.count>0? ActualHeight(86)+ActualHeight(50)*model.TT_RO_REPAIR_PART.count+2:0);
        return ActualHeight(88)+detailViewHeight;
    } else {
        return ActualHeight(88);
    }
}

- (void)reloadLocalData:(ICMRepairOrderArrayModel *)dataArray {
    self.searchLocalArray=nil;
    

    self.searchLocalArray = dataArray;
    self.searchLocalArray.TT_REPAIR_ORDER=[dataArray.TT_REPAIR_ORDER mutableCopy];
 
    [self.localTableView reloadData];
}

#pragma mark -- LocalRepairTableViewCellDeleagte
- (void)showRepairDetail:(LocalRepairTableViewCell *)cell {
    NSIndexPath *indexPath = [self.localTableView indexPathForCell:cell];
    
    for(int i = 0;i<self.searchLocalArray.TT_REPAIR_ORDER.count;i++)
    {
        if(indexPath.row != i)
        {
            ICMRepairOrderModel *model = self.searchLocalArray.TT_REPAIR_ORDER[i];
            model.isShow = NO;
        }
    }
    [self.localTableView reloadData];
}

@end
